//
//  FirstTwoViewController.m
//  DaJiHui
//
//  Created by Hao on 2017/9/11.
//  Copyright © 2017年 Hao. All rights reserved.
//
#import "FirstModel.h"
#import "FirstTwoTableViewCell.h"
#import "FirstTwoViewController.h"
#import "AFNetworking.h"
#import "FirstTwoBannerModel.h"
#import "FirstTwoBannerCell.h"
#import "FirstTwoShowCell.h"
#import "FirstTwoContenCell.h"
#import "FirstThrViewController.h"
@interface FirstTwoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSString *str;
@property (nonatomic, strong) NSArray *bannerArr;
@property (nonatomic, strong) FirstTwoBannerModel *model;
@property (nonatomic, strong) NSMutableArray *showArr;
@end

@implementation FirstTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bannerArr = [NSMutableArray array];
    self.showArr = [NSMutableArray array];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager setResponseSerializer:[AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableContainers]];
    manager.requestSerializer.timeoutInterval = 10.0f;
    manager.requestSerializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithArray:@[@"GET", @"POST",@"HEAD"]];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json",@"text/javascript",nil];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.chaojihui123.com/phonedata.php?s=/Index/store/id/%@",self.dic] ];
    __weak typeof(self) weakSelf = self;
    [manager POST:url.absoluteString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (responseObject && responseObject[@"picture"] && [responseObject[@"picture"] isKindOfClass:[NSArray class]]) {
                weakSelf.bannerArr = [NSArray arrayWithArray:responseObject[@"picture"]];
            }
            if (responseObject && responseObject[@"stres"] && [responseObject[@"stres"] isKindOfClass:[NSDictionary class]]) {
                weakSelf.model = [FirstTwoBannerModel modelWithDic:responseObject[@"stres"]];
            }
            if (responseObject && responseObject[@"res"] && [responseObject[@"res"] isKindOfClass:[NSArray class]]) {
                weakSelf.showArr = [FirstTwoResModel transformWithArry:responseObject[@"res"]];
            }
            [weakSelf.myTableView reloadData];
            
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    NSLog(@"%@",self.dic);
    [self makeBaseUI];
    
    // Do any additional setup after loading the view.
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==1) {
        return 40;
    }
    if (section == 3) {
        return 40;
    }
    
    return  CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==1) {
        UIView * scrBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ZTY_SCREEN_WIDTH, 20)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 2.5, ZTY_SCREEN_WIDTH, 20)];
        scrBackView.backgroundColor = [UIColor whiteColor];
        label.text= @"商家简介";
        label.textColor = [UIColor getColorFromRGBString:@"#333333"];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:15];
        [scrBackView addSubview:label];
        return scrBackView;
    }
    
    if (section==3) {
        UIView * scrBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ZTY_SCREEN_WIDTH, 20)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 2.5, ZTY_SCREEN_WIDTH, 20)];
        scrBackView.backgroundColor = [UIColor whiteColor];
        label.text= @"全部商品";
        label.textColor = [UIColor getColorFromRGBString:@"#333333"];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:15];
        [scrBackView addSubview:label];
        return scrBackView;
    }
    
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section==3) {
        UIView * scrBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, ZTY_SCREEN_WIDTH, 20)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 2.5, ZTY_SCREEN_WIDTH, 20)];
        scrBackView.backgroundColor = [UIColor whiteColor];
        label.text= @"没有更多了......";
        label.textColor = [UIColor getColorFromRGBString:@"#333333"];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:15];
        [scrBackView addSubview:label];
        return scrBackView;
    }
    
    return nil;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 3) {
        return 40;
    }
    return CGFLOAT_MIN;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section==0) {
        return 1;
    }else if (section == 1) {
        return _bannerArr.count;
    }else if (section == 2){
        return 1;
    }else if (section==3){
        return _showArr.count;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 300;
    }else if(indexPath.section == 1){
        return 300;
    }else if(indexPath.section == 2){
        CGFloat height = 0;
        if (self.model) {
            height = [self.model.content boundingRectWithSize:CGSizeMake(ZTY_SCREEN_WIDTH-30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height;
        }
        return height;
    }else if(indexPath.section==3){
        return 110;
    }else{
        return 300;
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    if (indexPath.section == 0) {
        FirstTwoBannerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FirstTwoBannerCell"];
        if (!cell) {
            cell = [[FirstTwoBannerCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"FirstTwoBannerCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.JumpGoosHandler = ^{
            [weakSelf.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2] atScrollPosition:UITableViewScrollPositionNone animated:YES];
        };
        cell.model= self.model;
        return cell;
    } else if (indexPath.section == 1){
        FirstTwoShowCell * cell = [tableView dequeueReusableCellWithIdentifier:@"FirstTwoShowCell"];
        if (!cell) {
            cell = [[FirstTwoShowCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FirstTwoShowCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.iconUrl = self.bannerArr[indexPath.row];
        return cell;
    }else if (indexPath.section == 2){
        FirstTwoContenCell * cell = [tableView dequeueReusableCellWithIdentifier:@"FirstTwoContenCell"];
        if (!cell) {
            cell = [[FirstTwoContenCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FirstTwoContenCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.content = self.model.content;
        return cell;
    } else if(indexPath.section == 3){
        FirstTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"FirstTwoTableViewCell"];
        if (!cell) {
            cell = [[FirstTwoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FirstTwoTableViewCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.myModel = self.showArr[indexPath.row];
        return cell;
    }else{
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 3)
    {
        FirstThrViewController *vc = [[FirstThrViewController alloc]init];
        FirstTwoResModel *model=(FirstTwoResModel *)self.showArr[indexPath.row];
        vc.dic = model.myid;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}
- (void)makeBaseUI{
    
    [self.view addSubview:self.myTableView];
    
}

- (UITableView *)myTableView{
    
    if (_myTableView == nil) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, ZTY_SCREEN_WIDTH, ZTY_SCREEN_HEIGHT-49-64) style:UITableViewStyleGrouped];
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
//        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.sectionFooterHeight = 0;
    }
    
    return _myTableView;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [self.myTableView reloadData];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}/*
  #pragma mark - Navigation
  
  // In a storyboard-based application, you will often want to do a little preparation before navigation
  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  // Get the new view controller using [segue destinationViewController].
  // Pass the selected object to the new view controller.
  }
  */

@end

